package com.heroCW.bodhiAttic.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heroCW.bodhiAttic.entity.Category;
import com.heroCW.bodhiAttic.resultUtil.Result;
import com.heroCW.bodhiAttic.resultUtil.ResultCode;
import com.heroCW.bodhiAttic.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类管理
 */
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     *
     * @param category
     * @return
     */
    @PostMapping
    public Result<String> save(@RequestBody Category category) {
        log.info("category: {}", category);
        boolean flag = categoryService.save(category);
        if (flag) {
            return Result.success(ResultCode.SAVE_OK, "新增分类成功");
        } else {
            return Result.error(ResultCode.SAVE_ERR, "新增分类失败");
        }
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public Result<IPage<Category>> page(int page, int pageSize) {

        // 进行分页查询
        IPage<Category> categoryIPage = categoryService.getPage(page, pageSize);

        // 删除一个并重新查询时，如果不够分页，则以页数最大值再查询一次
        if (page > categoryIPage.getPages()) {
            categoryIPage = categoryService.getPage((int) categoryIPage.getPages(), pageSize);
        }

        if (categoryIPage != null) {
            return Result.success(ResultCode.GET_OK, categoryIPage);
        } else {
            return Result.error(ResultCode.GET_ERR, "没有找到分类数据");
        }
    }

    /**
     * 根据ID删除分类
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result<String> delete(Long ids) {
        log.info("删除分类：id为：{}", ids);

        // categoryService.removeById(ids);
        Boolean flag = categoryService.remove(ids);
        if (flag) {
            return Result.success(ResultCode.DELETE_OK, "分类信息删除成功");
        } else {
            return Result.error(ResultCode.DELETE_ERR, "分类信息删除失败");
        }
    }

    /**
     * 根据id修改分类信息
     *
     * @param category
     * @return
     */
    @PutMapping
    public Result<String> update(@RequestBody Category category) {
        log.info("修改分类信息：{}", category);

        boolean flag = categoryService.updateById(category);
        if (flag) {
            return Result.success(ResultCode.UPDATE_OK, "修改分类信息成功");
        } else {
            return Result.error(ResultCode.UPDATE_ERR, "修改分类信息失败");
        }
    }

    /**
     * 根据分类条件查询数据
     *
     * @param category
     * @return
     */
    @GetMapping({"/front/list", "/backend/list"})
    public Result<List<Category>> list(Category category) {

        Integer type = category.getType();

        List<Category> list = categoryService.getByType(type);

        if (list != null) {
            return Result.success(ResultCode.GET_OK, list);
        } else {
            return Result.error(ResultCode.GET_ERR, "没有找到分类数据");
        }
    }
}
